package com.tarena.lbs.marketing.web.repository;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.marketing.web.mapper.CouponCodeMapper;
import com.tarena.lbs.pojo.marketing.po.CouponCodePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CouponCodeRepository extends ServiceImpl<CouponCodeMapper, CouponCodePO> {
    @Autowired
    private CouponCodeMapper couponCodeMapper;
    public CouponCodePO getOneCouponCode(Integer couponId) {
        //select * from coupon_code where coupon_id=? and status=0 limit 1
        QueryWrapper<CouponCodePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id", couponId).eq("status", 0);
        PageHelper.startPage(1,1);
        List<CouponCodePO> pos = couponCodeMapper.selectList(queryWrapper);
        PageInfo.of(pos);
        if (CollectionUtils.isNotEmpty(pos)){
            return pos.get(0);
        }else{
            return null;
        }
    }
    public void updateCouponCodeStatus(Integer couponCodeId, Integer status) {
        //0 未领取 1 已领取 update coupon_code set status=#{status} where id=#{couponCodeId}
        CouponCodePO couponCodePO = new CouponCodePO();
        couponCodePO.setId(couponCodeId);
        couponCodePO.setStatus(status);
        couponCodeMapper.updateById(couponCodePO);
    }
}
